//
//  ViewController.m
//  05 - HomeWork
//
//  Created by 李昊林 on 16/7/20.
//  Copyright © 2016年 李昊林. All rights reserved.
//

#import "ViewController.h"
#import "AppDelegate.h"
#define FRAME [UIScreen mainScreen].bounds
#define WIDTH FRAME.size.width
#define HEIGHT FRAME.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic,retain) UIScrollView *scrollView;
@property (nonatomic,retain) UIScrollView *scrollViewOfAlbum;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeAlbum];
    [self addPage];
    // Do any additional setup after loading the view, typically from a nib.
//    完善ScrollView相册, 实现轮播及缩放恢复。
//    封装轮播图
//    添加UIPageControl, 绑定事件, 有点击切换效果
//    不用添加定时器

}

- (void)makeAlbum {
    UIScrollView *album = [[UIScrollView alloc] initWithFrame:FRAME];
    [self.view addSubview:album];
    NSArray *arr = [NSArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", nil];
    album.contentSize = CGSizeMake(WIDTH * arr.count, HEIGHT);
    album.pagingEnabled = YES;
    album.bounces = NO;
    album.delegate = self;
    for (NSInteger i = 0; i < arr.count ; i++) {
        UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        smallScrollView.delegate = self;
        img.image = [UIImage imageNamed:arr[i]];
        [smallScrollView addSubview:img];
        [album addSubview:smallScrollView];
        smallScrollView.tag = i;
        smallScrollView.minimumZoomScale = 0.5;
        smallScrollView.maximumZoomScale = 2;
    }
    album.contentOffset = CGPointMake(WIDTH, 0);
    album.bounces = NO;
    album.showsHorizontalScrollIndicator = NO;
}
- (void)addPage{
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, HEIGHT - 80, 100, 40)];
    self.page.currentPage = 0;
    self.page.numberOfPages = 3;
    [self.view addSubview:self.page];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    _index = scrollView.tag;
    return [scrollView.subviews firstObject];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.page.currentPage = index - 1;
    if (index == 3) {
        self.page.currentPage = 0;
        scrollView.contentOffset = CGPointMake(0, 0);
    }
    if (index == 0) {
        self.page.currentPage = 3;
        scrollView.contentOffset = CGPointMake(WIDTH * 3, 0);
    }
    UIScrollView *scroll = [scrollView.subviews objectAtIndex:_index];
    scroll.zoomScale = 1;
}
- (void)pageAction:(UIPageControl *)page{
    // 点击换图
    [self.scrollViewOfAlbum setContentOffset:CGPointMake(WIDTH * (self.page.currentPage), 0) animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
